package org.xq.shop.maxaishop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xq.shop.maxaishop.entity.XqCommodity;
import org.xq.shop.maxaishop.service.XqCommodityService;
import org.xq.shop.maxaishop.util.JsonRes;
import org.xq.shop.maxaishop.util.Result;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (XqCommodity)表控制层
 *
 * @author makejava
 * @since 2021-06-11 15:53:11
 */
@Api( tags = "商品展示与管理")
@RestController
@RequestMapping("xqCommodity")
public class XqCommodityController {
    /**
     * 服务对象
     */
    @Resource
    private XqCommodityService xqCommodityService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据主键查询单个商品信息")
    @ApiImplicitParam(name = "id", value = "商品id", required=true,paramType="query" )
    @GetMapping("selectOne")
    public XqCommodity selectOne(Integer id) {
        return this.xqCommodityService.queryById(id);
    }

    /**
     * 查询商品数据(前台展示)
     *
     * @param
     * @return
     */
    @ApiOperation("展示所有在售商品")
    @GetMapping("selectAll")
    public List<XqCommodity> selectAll(){
        List<XqCommodity> xqCommodities = this.xqCommodityService.selectAll();
        return xqCommodities;
    }

    /**
     * 查询销量前十
     * @return
     */
    @ApiOperation("展示销量前十商品")
    @PostMapping("selectFruitSales")
    public List<XqCommodity> selectFruitSales(){
        return this.xqCommodityService.selectFruitSales();
    }


    @ApiOperation("展示销量前十商品")
    @PostMapping("selectVegetableSales")
    public List<XqCommodity> selectVegetableSales(){
        return this.xqCommodityService.selectVegetableSales();
    }

    /**
     * 查询在售商品(后台数据)
     * @param page
     * @param limit
     * @param uname
     * @return
     */
    @ApiOperation("商品列表信息")
    @GetMapping( "selectFruit")
    public Result<XqCommodity> selectFruit(Integer page, Integer limit, String uname){
        int start = (page-1)*limit;
        Result<XqCommodity> result = new Result<XqCommodity>();
        if (uname==null){
            List<XqCommodity> xqCommodities = this.xqCommodityService.queryFruit(start, limit);
            int count = this.xqCommodityService.selectCount();
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCommodities);

        }else {
            List<XqCommodity> xqCommodities = this.xqCommodityService.selectByUname(uname, start, limit);
            int count = this.xqCommodityService.selectCount();
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCommodities);
        }
        return  result;
    }

    @ApiOperation("下架商品列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页面",required=true,paramType="query"),
            @ApiImplicitParam(name="limit",value="每页行数",required=true,paramType="query"),
            @ApiImplicitParam(name="sname",value="商品名称",paramType="query",dataType="String")
    })
    @GetMapping("selectXjFruit")
    public Result<XqCommodity> selectXjFruit(Integer page, Integer limit, String sname){
        int start = (page-1)*limit;
        Result<XqCommodity> result = new Result<XqCommodity>();
        if (sname==null){
            List<XqCommodity> xqCommodities = this.xqCommodityService.queryXjFruit(start, limit);
            int count = this.xqCommodityService.selectXjCount();
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCommodities);

        }else {
            List<XqCommodity> xqCommodities = this.xqCommodityService.selectByXjUname(sname, start, limit);
            int count = this.xqCommodityService.selectXjCount();
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCommodities);
        }
        return  result;
    }





    /**
     * 通过id删除
     * @param cid
     * @return
     */
    @ApiOperation("通过商品id下架商品")
    @GetMapping("deleteById")
    public boolean deleteById(Integer cid){
        boolean b = this.xqCommodityService.deleteById(cid);
        return b;
    }

    /**
     * 添加数据
     * @param aname
     * @param comImg
     * @param category
     * @param introd
     * @param com_stork
     * @param com_original
     * @return
     */
    @ApiOperation("添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="aname",value="商品名",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="comImg",value="商品图片",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="category",value="类别",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="introd",value="介绍",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="com_stork",value="库存",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="com_original",value="原价",required=true,paramType="query",dataType="String")
    })
    @PostMapping("insert")
  public int insert(String aname,String comImg,String category,String introd,String com_stork,String com_original,String com_cb){
        XqCommodity xqCommodity = new XqCommodity();
        xqCommodity.setComImg(comImg);
        xqCommodity.setComName(aname);
        xqCommodity.setCategory(category);
        xqCommodity.setIntrod(introd);
        xqCommodity.setComStork(Integer.parseInt(com_stork));
        xqCommodity.setComOriginal(Double.parseDouble(com_original));
        xqCommodity.setComCb(Double.parseDouble(com_cb));
        int i = this.xqCommodityService.insert(xqCommodity);
        return i;
    }

    /**
     * 通过商品id查询信息
     * @param cid
     * @return
     */
    @ApiOperation("根据主键查询单个商品信息")
    @ApiImplicitParam(name = "cid", value = "商品id", required=true,paramType="query" )
    @GetMapping("queryById")
    public XqCommodity queryById(Integer cid){
        return this.xqCommodityService.queryById(cid);
    }



    /**
     * 购买
     * @param sales
     * @param stork
     * @param cid
     * @return
     */
    @ApiOperation("商品减库,加销量操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sales",value="销量",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="stork",value="售卖数量",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="cid",value="商品id",required=true,paramType="query",dataType="String"),
    })
    @PostMapping("updateSales")
    public int updateSales(int sales,int stork,int cid){
        int i = this.xqCommodityService.updateSales(sales, stork, cid);
        return i;
    }

    /**
     * 商品修改
     * @param xqCommodity
     * @return
     */
    @ApiOperation("修改商品信息")
    @PostMapping("update")
    public Map update(XqCommodity xqCommodity){
        int update = this.xqCommodityService.update(xqCommodity);
        if (update==1){
            return JsonRes.success(update);
        }else {
            return JsonRes.failure(null);
        }
    }

    /**
     * 减库存
     * @param cid
     * @return
     */
    @ApiOperation("上架操作")
    @ApiImplicitParam(name="cid",value="商品id",required=true,paramType="query",dataType="String")
    @GetMapping("updateStatus")
    public Map updateStatus(Integer cid){
        int i = this.xqCommodityService.updateStatus(cid);
        if (i==1){
            return JsonRes.success(i);
        }else {
            return JsonRes.failure(null);
        }

    }

    /**
     * 选中下架
     * @param ids
     * @return
     */
    @ApiOperation("批量下架")
    @ApiImplicitParam(name="ids",value="商品id,以,隔开",required=true,paramType="query",dataType="String")
    @PostMapping ("XzDel")
    public int XzDel(String ids){
        String[] split = ids.split(",");
        for (String str:split) {
           this.xqCommodityService.deleteById(Integer.parseInt(str));
        }
        return 200;
    }

    /**
     * 选中删除
     * @param ids
     * @return
     */
    @ApiOperation("批量上架")
    @ApiImplicitParam(name="ids",value="商品id,以,隔开",required=true,paramType="query",dataType="String")
    @PostMapping ("XzSj")
    public int XzSj(String ids){
        String[] split = ids.split(",");
        for (String str:split) {
            this.xqCommodityService.updateStatus(Integer.parseInt(str));
        }
        return 200;
    }


}
